package com.springmq.mq.juc;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName : Demo2
 * @Author : lby
 * @Date: 2021/2/4 11:11
 * @Description :
 */
@Slf4j
public class Demo2 {
    public static void main(String[] args) {
        // arraylist 当使用在多线程时容易出现并发问题
        List<String> objects = new ArrayList<>();
        // 使用collection中的集合可以避免
        List<Object> list = Collections.emptyList();
        for (int i = 0; i < 10; i++) {
            ThreadFactory threadFactory = r -> new Thread(r, "aaaaa" + r.hashCode());
            ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 10, TimeUnit.MINUTES, new SynchronousQueue<Runnable>(), threadFactory);
            executor.execute(() -> {
                objects.add("bbbbbb");
                System.out.println("数据"+objects);
                log.info("当前集合:{}", objects);
            });
        }
    }
}
